import { getChannel, isWeixin } from "@/utils/channel";
import { ChannelEnum } from "@/enums/channelEnum";
import { PayStatusEnum } from "@/enums/payEnum";

const alipay = async (params: any) => {
    const channelVal = getChannel();

    // 支付宝不支持微信环境,可跳转提示页面
    if (isWeixin()) return;

    try {
        switch (channelVal) {
            case channelVal == ChannelEnum.H5:
                return new Promise((resolve) => {
                    // 支付宝H5支付，前端表单提交
                    resolve({ callPayStatus: PayStatusEnum.PENDING, formData: params.formData })
                })
                break;
            case channelVal == ChannelEnum.APP:
                return new Promise((resolve, reject) => {
                    uni.requestPayment({
                        provider: 'alipay',
                        orderInfo: params.orderInfo,
                        success(res) {
                            res.callPayStatus = PayStatusEnum.SUCCESS
                            resolve(res)
                        },
                        fail(res) {
                            res.callPayStatus = PayStatusEnum.FAIL
                            reject(res)
                        }
                    })
                })
                break;
            default:
                break;
        }
    } catch (error) {
        error.callPayStatus = PayStatusEnum.ERROR
        return Promise.reject(error)
    }
}

export default alipay;